﻿using ComputationalGeometry.Shapes;
using NUnit.Framework;

namespace ComputationalGeometry.UnitTests
{
    [TestFixture]
    public class MathUtiltyTest
    {
        [Test]
        public void CrossProductTest()
        {
            Point2D p1 = new Point2D(3, 3);
            Point2D p2 = new Point2D(4, 4);
            Point2D p3 = new Point2D(4, 5);
            Point2D p4 = new Point2D(5, 4);
            Point2D p5 = new Point2D(5, 5);

            // test that left turn has positive sign
            double result = MathUtility.CrossProduct(p1, p2, p3);
            Assert.True(result > 0, string.Format("Cross product result was {0}", result));

            // test that right turn has negative sign
            result = MathUtility.CrossProduct(p1, p2, p4);
            Assert.True(result < 0, string.Format("Cross product result was {0}", result));

            // test that no turn is zero
            result = MathUtility.CrossProduct(p1, p2, p5);
            Assert.True(result == 0, string.Format("Cross product result was {0}", result));
        }

        [Test]
        public void DoLinesIntersectTest()
        {
            Point2D p1 = new Point2D(3, 3);
            Point2D p2 = new Point2D(6, 6);
            Point2D p3 = new Point2D(6, 3);
            Point2D p4 = new Point2D(3, 6);

            Line2D s1 = new Line2D(p1, p2);
            Line2D s2 = new Line2D(p3, p4);
            bool intersect = MathUtility.DoLinesIntersect(s1, s2);
            Assert.IsTrue(intersect);

            s1 = new Line2D(p1, p3);
            s2 = new Line2D(p2, p4);
            intersect = MathUtility.DoLinesIntersect(s1, s2);
            Assert.IsFalse(intersect);

            s1 = new Line2D(p1, p4);
            s2 = new Line2D(p2, p3);
            intersect = MathUtility.DoLinesIntersect(s1, s2);
            Assert.IsFalse(intersect);
        }
    }
}
